package client.view;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: sam
 * Date: 07/04/12
 * Time: 16:02
 * To change this template use File | Settings | File Templates.
 */

public class HelpPanel extends JPanel {

    JTabbedPane tabbedPane;
    Font bigFont;
    Color labelColor;

    public HelpPanel() {

        setLayout(new GridLayout(1, 1));

        bigFont = new Font("sanserif", Font.BOLD, 14);
        labelColor = new Color(0,104,139);

        tabbedPane = new JTabbedPane( JTabbedPane.TOP );
        tabbedPane.setTabLayoutPolicy( JTabbedPane.CENTER );
        Font font = new Font("Dialog", Font.BOLD, 11);
        tabbedPane.setFont(font);

        tabbedPane.addTab("Getting Started", null, new HelpIntroPanel(), "");
        tabbedPane.addTab("About Repositories", null, new HelpRepositoriesPanel(), "");
        tabbedPane.addTab("Downloading", null, new HelpDownloadPanel(), "");
        tabbedPane.addTab("Uploading", null, new HelpUploadPanel(), "");
        tabbedPane.addTab("User Settings", null, new JPanel(), "");

        add(tabbedPane);

    }

    public class HelpIntroPanel extends JPanel {

        JScrollPane scrollPane;

        public HelpIntroPanel() {

            setLayout(new GridLayout(1,1));

            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

            JPanel panel;
            JTextArea textArea;
            JLabel label;

            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("Music-alms\n");
            JLabel introLabelSub = new JLabel("A Sound Donation");
            introLabelSub.setFont(new Font("sanserif", Font.BOLD, 11));
            introLabelSub.setAlignmentX(JLabel.CENTER_ALIGNMENT);

            //JLabel labelA = getLabel("What is Music-alms?\n");
            textArea.setText("An art form consisting of sequences of sounds in time - charitable donations of money or goods to the poor or needy." +
                    " Music-alms allows groups of friends to donate & download their music amongst each other.");

            panel.add(label);
            panel.add(introLabelSub);
            mainPanel.add(panel);
            panel.add(textArea);

            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("Getting started\n");
            textArea.setText("If you have a username and password, you can login to your repository, then start donating & downloading." +
                    "If you dont yet belong to a repository, please see the 'Repositories' help section on how to create or join a repository.");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);

            scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.getVerticalScrollBar().setUnitIncrement(16);

            add(scrollPane);
        }
    }

    public class HelpUploadPanel extends JPanel {

        JScrollPane scrollPane;

        public HelpUploadPanel() {

            setLayout(new GridLayout(1,1));

            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

            JPanel panel;
            JTextArea textArea;
            JLabel label;

            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("How do i donate a song?\n");

            textArea.setText("\nIn the 'upload' panel, you can browse and choose songs from your local machine. Valid file types are: " +
                    "mp3, wav, m4a, m4p, wma.  Once you have selected a song from your machine, it will appear in the upload table.\n");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);


            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("How do set song and artist name?\n");

            textArea.setText("\nOnce you have your songs in the upload table, you can directly edit the 'Track' and 'Artist' cells by double clicking on them and" +
                    "entering text.  In order to speed up this process, you can use the auto complete functionality. This " +
                    "will automatically fill out the 'Track' and 'Artist' fields, if your file name is in the format 'track - artist' and 'artist - track'." +
                    "You can do this by selecting one or more songs and choosing the option you want.  \n");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);

            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("How do set the genre?\n");

            textArea.setText("\nUnlike 'Track' and 'Artist', the 'Genre' cell is not editable, you must select the song in the upload table and then" +
                    " select the genre it belongs to in the 'Genres' menu beneath.  If there are no genres that your song matches, you" +
                    "can add your own genre.  This is done by supplying a genre name and a description.  The genre name must be one word, with no spaces.\n");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);

            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("Uploading & Validation\n");

            textArea.setText("\nOnce you are happy with the uploads table (i.e. name, artist, genre are complete), you are ready to upload. " +
                    "You can select all of your donations in the upload table (shift and selection for multiple uploads), and then hit the 'Upload' button." +
                    "Music-alms has the following validation criteria:\n\n" +
                    "File name can be any valid Windows, Mac or Linux file path, plus any criteria in the 'Track & Artist' validation. \n" +
                    "Track & Artist name: alpha numeric characters, whitespace, hyphen (-) and periods (.), brackets (()) and a valid file type.\n\n" +
                    "If any of the data you have supplied is invalid, Music-alms will display an alert dialog");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);

            scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.getVerticalScrollBar().setUnitIncrement(16);

            add(scrollPane);
        }
    }

    public class HelpRepositoriesPanel extends JPanel {

        JScrollPane scrollPane;

        public HelpRepositoriesPanel() {

            setLayout(new GridLayout(1,1));

            JPanel mainPanel = new JPanel();

            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

            JPanel panel;
            JTextArea textArea;
            JLabel label;


            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("What are repositories?\n");

            textArea.setText("\nRepositories are two things; They are locations where songs are physically stored and backed-up.  " +
                    "They also represent a discrete group of users. A repository is like a private library of music " +
                    "that only a certain group of members can access.\n");


            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);

            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("How do i create a repository?\n");

            textArea.setText("\nYou can create a repository by making a repository request to music-alms.  This is done in the 'reposotories' panel under " +
                    "the 'create' panel on the right.  You will need to provide:\n\n" +
                    "1) A URL to an FTP server.\n" +
                    "2) A repository path or file location on the FTP server.\n" +
                    "3) Authentication details (username and password) for your FTP server (these are encrypted and not visible to music-alms).\n\n" +
                    "Music-alms will then get back to you, informing you whether your repository request was successful or not.");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);


            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("How do i join a repository?\n");

            textArea.setText("\nThe repository administrator decides who has access to their library (as it is they who " +
                    "set-up and host the repository).  You can make a repository join request.  If approved (by both Music-alms & the repository administrator) " +
                    ", you are subsequently allowed access to that specific repository\n");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);


            scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.getVerticalScrollBar().setUnitIncrement(16);

            add(scrollPane);
        }
    }

    public class HelpDownloadPanel extends JPanel {

        JScrollPane scrollPane;

        public HelpDownloadPanel() {

            setLayout(new GridLayout(1,1));

            JPanel mainPanel = new JPanel();

            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

            JPanel panel;
            JTextArea textArea;
            JLabel label;


            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("How do download genres?\n");

            textArea.setText("\nGenres are retrieved automatically upon successful login (see 'Getting Started' help section)." +
                    " Genres can be created (added) to a repository once a user is a member of that repository.\n");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);



            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("How do i search a genre?\n");

            textArea.setText("\nSelect the genre you want to search and hit the 'Find' button underneath the genre list.  If you want to" +
                    " search a genre for a particular song name and/or artist, then enter these in to the fields" +
                    " provided under the genre list and then hit 'Find' \n");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);



            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("How do i download a song?\n");

            textArea.setText("\nBy selecting the song you want and hitting the 'Download' button underneath." +
                    " If you want to download multiple songs, then hold down the shift key when making your multiple download section.\n");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);



            panel = getPanel();
            textArea = getTextArea();
            label = getLabel("Where do my downloads go?\n");

            textArea.setText("\nBy default, songs download are saved one level (folder/directory) 'up' from the location from the Music-alms download (executable)." +
                    " If you want to change the location of downloaded songs, you can do this in the 'Settings' panel (under 'Download Location').\n");

            panel.add(label);
            panel.add(textArea);
            mainPanel.add(panel);


            scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.getVerticalScrollBar().setUnitIncrement(16);

            add(scrollPane);
        }
    }

    public JTextArea getTextArea() {
        JTextArea textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        return textArea;
    }

    public JLabel getLabel(String txt){
        JLabel label = new JLabel(txt);
        label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        label.setForeground(labelColor);
        label.setFont(bigFont);
        return label;
    }

    public JPanel getPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        return panel;
    }
}
